package com.jsecode.protocol.gid22;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jsecode.protocol.gid22.entity.GidCANStatusFlag;
import com.jsecode.protocol.gid22.entity.GidDSFlag;

import io.netty.buffer.ByteBuf;

/**
 * CAN静态数据包 0x01
 *
 * @author grind
 * @date Apr 1, 2014 10:57:16 AM
 * @version 1.0
 */
public class GidCan extends GidBody {
    protected static Logger logger = LoggerFactory.getLogger("gid22-GidCan");
    
    private Date time; // 6 个字节， 具体定义见下面
                        //    1   RTC Hour    1   u8
                        //    2   RTC Minute  1   u8
                        //    3   RTC Seconds 1   u8
                        //    4   RTC Year    1   u8
                        //    5   RTC Month   1   u8
                        //    6   RTC Day 1   u8
    private byte statusMask; // STATUS MASK 1   u8          汽车状态掩码，表示8类汽车状态支持与否
    private byte[] dsMask; // DS     MASK    5   u32         汽车数据流掩码，表示40个数据流支持与否
    private int packNum; //Pack Number 1   u8          n,状态和数据流 循环数据包 包数
    
    // 进一步解析需要的数据
    private List<GidCANStatusFlag> statusFlagList = new ArrayList<GidCANStatusFlag>();
    private List<GidDSFlag> dsFlagList = new ArrayList<GidDSFlag>();
    
    public GidCan(int bodyLen){
        super(bodyLen);
    }

    @Override
    public int unpack(ByteBuf srcBuf){
        int retVal = 0;
        try {
            time = GidUtil.readDate6(srcBuf);
            statusMask = srcBuf.readByte();
            dsMask = srcBuf.readBytes(5).array();
            packNum = srcBuf.readUnsignedByte();

            for (int index = 0; index < packNum; index++) {
                GidCANStatusFlag statusFlag = new GidCANStatusFlag(statusMask);
                GidDSFlag dsFlag = new GidDSFlag(dsMask);
                retVal = statusFlag.unpack(srcBuf);
                if (retVal != -1) {
                    retVal = dsFlag.unpack(srcBuf);
                }
                if (retVal == 0) {
                    statusFlagList.add(statusFlag);
                    dsFlagList.add(dsFlag);
                }
            }
        } catch (Exception e) {
            retVal = -1;
            logger.error("unpack can data with error:" + e.getMessage());
        }
        
        return retVal;
    }
    
    public Date getTime(){
        return time;
    }

    public void setTime(Date time){
        this.time = time;
    }

    public byte getStatusMask(){
        return statusMask;
    }

    public void setStatusMask(byte statusMask){
        this.statusMask = statusMask;
    }

    public byte[] getDsMask(){
        return dsMask;
    }

    public void setDsMask(byte[] dsMask){
        this.dsMask = dsMask;
    }

    public int getPackNum(){
        return packNum;
    }

    public void setPackNum(int packNum){
        this.packNum = packNum;
    }

    public List<GidCANStatusFlag> getStatusFlagList(){
        return statusFlagList;
    }

    public void setStatusFlagList(List<GidCANStatusFlag> statusFlagList){
        this.statusFlagList = statusFlagList;
    }

    public List<GidDSFlag> getDsFlagList(){
        return dsFlagList;
    }

    public void setDsFlagList(List<GidDSFlag> dsFlagList){
        this.dsFlagList = dsFlagList;
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("time:").append(time).append(",");
        sb.append("packNum:").append(packNum).append(",");
        sb.append("dsFlagList=[");
        for (GidDSFlag dsFlag:dsFlagList) {
            sb.append("[");
            sb.append(dsFlag);
            sb.append("]");
        }
        sb.append("]");
        return sb.toString();
    }

}
